﻿using System;
using System.Collections.Generic;

namespace MarketPlace.Web.Models
{
    public interface IClientType
    {
        string ToJsonString();
    }

    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class ClientMessage : IClientType
    {

        private bool success = true;

        public bool Success
        {
            get
            {
                return success && ((this.Errors == null) || (this.Errors.Count == 0));
            }
            set
            {
                success = value;
            }
        }

        public string Title { get; set; }

        public string Msg { get; set; }

        private List<string> errors;

        public List<string> Errors
        {
            get
            {
                if (null == errors)
                    errors = new List<string>();

                return errors;
            }
            set
            {
                errors = value;
            }
        }

        public string ToJsonString()
        {
            string msg = this.Msg;

            string tpl = "Success:{0},Title:'{1}',Msg:'{2}'";

            if (null != Errors && Errors.Count != 0)
            {
                msg += "<ul>";

                foreach (string err in Errors)
                {
                    msg += string.Format("<li>{0}</li>", err);
                }

                msg += "</ul>";
            }

            return "{" + string.Format(tpl, this.Success.ToString().ToLower(), this.Title, msg) + "}";
        }
    }

    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class ScriptClientMessage : ClientMessage
    {
        public bool Scripts
        {
            get
            {
                return !string.IsNullOrEmpty(this.Src);
            }
        }

        public string Src { get; set; }
    }
}
